# coding: utf-8
from math import sqrt

#欧几里得距离
def sim_distance( prefs,person1,person2 ):
    si={}
    for itemId in prefs[person1]:
        if itemId in prefs[person2]:
            si[itemId] = 1
    #no same item
    if len(si)==0: return 0
    sum_of_squares = 0.0    
    
    #计算距离 
    sum_of_squares=sum([pow(prefs[person1][item]-prefs[person2][item],2) for item in si])
    return 1/(1 + sqrt(sum_of_squares) )

prefs = {'User1': {'apps':2.5,'start_count':3.0,'runtime':1.8,'net_cost':2.6},'User2':{'apps':3.2,'start_count':1.3,'runtime':0.9,'net_cost':2.1}}
print sim_distance(prefs, 'User1', 'User2')